//
//  JXHTTPTool.swift
//  weibo-new
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 gjx. All rights reserved.
//

import AFNetworking

class JXHTTPTool: AFHTTPSessionManager {

    // let是线程安全的
    static let shareInstance : JXHTTPTool = {
        let tool = JXHTTPTool()
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tool
    }()
    
}

extension JXHTTPTool {
    func get(url: String, paras: ([String: Any]), successRes: ((_ json: Any?) -> Void)?, failureRes:((_ error: Error) -> Void)?) {
        
        _ = get(url, parameters: paras, progress: nil, success: { (_, jsonResult: Any?) in
            if let successRes = successRes {
                successRes(jsonResult)
            }
        }) { (_, errorResult: Error) in
            if let failureRes = failureRes {
                failureRes(errorResult)
            }
        }
    }
    
    
    func post(url: String, paras: ([String: Any]), successRes: ((_ json: Any?) -> Void)?, failureRes:((_ error: Error) -> Void)?) {
        
        _ = post(url, parameters: paras, progress: nil, success: { (_, jsonResult: Any?) in
            if let successRes = successRes {
                successRes(jsonResult)
            }
        }) { (_, errorResult: Error) in
            if let failureRes = failureRes {
                failureRes(errorResult)
            }
        }
    }
}
